#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 19-6-25 下午12:51
# @Author  : Tom
# @Site    : 
# @File    : goods.py
# @Software: PyCharm
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serilizers import Spu_goodsSerilizer, BrandSerilizer, GoodsCategorySerilizer
from meiduo_admin.utils import PageNumber_User


class GoodsView(ModelViewSet):
    serializer_class = Spu_goodsSerilizer
    queryset = SPU.objects.all()
    pagination_class = PageNumber_User
    permission_classes = [IsAdminUser, ]

    # 增加spu时候获取品牌名字
    def get_brand(self, requtest):
        brands = Brand.objects.all()
        brands_ser = BrandSerilizer(brands, many=True)
        return Response(brands_ser.data)

    # 增加spu时候，获取一级分类
    def get_category(self, request):
        category = GoodsCategory.objects.filter(parent=None)
        category_ser = GoodsCategorySerilizer(category, many=True)
        return Response(category_ser.data)

    # 增加spu时候获取二级分类和三级分类
    def get_categorys(self, request, pk):
        category_pk = GoodsCategory.objects.filter(parent_id=pk)
        category_pk_ser = GoodsCategorySerilizer(category_pk, many=True)
        return Response(category_pk_ser.data)
